<?php
class v1_manage{
    public function index(){
    	$session=$this->yum->library('session');
    	if($session->ISSESS()){
    		$role=$session->GVAR('role');
    		$purview=$session->GVAR('purview');
			if((count($purview)==1)&&(count($purview['manage'])==2)){
				$message=$this->yum->controller('message');
				$message->index(5,$app->config['Manage']['domain'].'index.php?c=manage&a=out','no','您没有任何权限,请联系管理员,5秒后自动退出.');
				exit();
			}else if(!empty($role)){
    		  header('Location: '.$this->yum->config['Manage']['domain'].'index.php?c=account&a=password');
    		  exit();
    		}
    	}
    	$this->yum->view('index.php');
    }
    public function login(){
		$captcha=$this->yum->library('captcha');
		if(!$captcha->check($this->yum->post('captcha',true),30)){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','您输入的验证码有误.');
			exit();
		}
		$user=strtolower($this->yum->post('user',true));
		$password=$this->yum->post('password',true);
		$member=$this->yum->model('member');
		$user=$member->GMember(array('where'=>array('user ='=>$user)));
		$user=reset($user);
		if(empty($user)){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','这个账户不存在,请确认后再登陆.');
			exit();
		}else if($user['password']!==$password){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','账户与密码不匹配.');
			exit();
		}else if($user['locked']!=0){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','账户被禁止登陆');
			exit();
		}else{
			$role=$this->yum->model('role');
			$temp=$role->GRole(array('where'=>array('id ='=>$user['role'],'locked ='=>0)));
			if(empty($temp)){
				$message=$this->yum->controller('message');
				$message->index(5,HTTP_REFERER,'no','您所在的角色已被限制登录,请联系管理员.');
				exit();
			}
			$member->UMember(array('set'=>array('visits=visits+'=>1,'ip ='=>ip2long($_SERVER["REMOTE_ADDR"])),'where'=>array('id ='=>$user['id'])));
			$session=$this->yum->library('session');
			$session->SVAR('member',$user['id']);
			$session->SVAR('role',$user['role']);
			$session->SVAR('name',$user['user']);
			$session->SVAR('visits',$user['visits']);
			$session->SVAR('ip',$user['ip']);
			$session->SVAR('date',$user['date']);
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'yes','登录成功,5秒后转到管理首页.');
		}
    }
    public function enrol(){
		if($this->yum->post('save')){
			$output=array('result'=>4,'msg'=>'');
			$user=$this->yum->post('user',true);
			$password=$this->yum->post('password',true);
			$affirm=$this->yum->post('affirm',true);
			$captcha=$this->yum->library('captcha');
			if(!$captcha->check($this->yum->post('captcha',true),30)){
				$output['msg']='您输入的验证码有误..';
			}else{
				if(preg_match('/^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/',$user,$match)==0){
					$output['msg']='请使用邮箱进行注册.';
				}else if($password!=$affirm){
					$output['msg']='输入的密码和确认密码不匹配.';
				}else{
					$member=$this->yum->model('member');
					if(count($member->GMember(array('where'=>array('user ='=>$user))))>0){
						$output['msg']='您输入的邮箱已被注册.';
					}else{
						$affected=$member->IMember(array('user'=>$user,'password'=>$password,'role'=>2,'ip'=>ip2long($_SERVER["REMOTE_ADDR"]),'date'=>$this->yum->runtime));
						if($affected==1){
							$output['result']=2;
							$output['msg']='注册成功.';
						}else{
							$output['msg']='请误重复提交,您已注册成功.';
						}
					}
				}
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$this->yum->view('enrol.php');
		}
    }
    public function captcha(){
    	$captcha=$this->yum->library('captcha');
		$captcha->showImage();
    }
    public function out(){
    	$session=$this->yum->library('session');
    	$session->destroy();
    	$message=$this->yum->controller('message');
		$message->index(5,domain,'yes','安全退出成功,5秒后转到登陆页.');
    }
}
?>